﻿using UnityEngine;
using System.Collections;
namespace SGUI
{
    public class UIRoot : MonoBehaviour
    {
        public Camera UICamera;
        Camera backCamera;
        public float Size;
        public static UIRoot Root;
        void Start()
        {
            Root = this;
            this.UICamera.orthographicSize = Size * 0.5f;
            CreateBackCamera();  
        }

        void CreateBackCamera() 
        {
            GameObject backCameraG = new GameObject("BackCamera");
            backCameraG.transform.parent = transform;
            backCamera = backCameraG.AddComponent<Camera>();
            backCamera.enabled = false;
            backCamera.orthographicSize = UICamera.orthographicSize;
            backCamera.clearFlags = CameraClearFlags.Color;
            backCamera.backgroundColor = new Color(0,0,0,0);
            backCamera.orthographic = true;
            backCamera.targetTexture = new RenderTexture((int)backCamera.orthographicSize * 2,(int)backCamera.orthographicSize*2,0);
        }

        public void ProcessTouches(Vector2 v2)
        {
            UIManager.Event.OnTouch(v2);
        }

        public void Update() 
        {
            if(Input.touchCount > 0)
            {
                ProcessTouches(Input.GetTouch(0).position);
            }
            if (Input.GetMouseButtonDown(0)) 
            {
                ProcessTouches(Input.mousePosition);
            }
        }
       
    }
}